package ltd.dujiabao.cmsservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.cmsservice.entity.CrmBanner;
import ltd.dujiabao.cmsservice.service.CrmBannerService;
import ltd.dujiabao.commonutils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author dujiabao
 * @since 2021-04-24
 */
@Api(value = "幻灯片后台管理")
@RestController
@RequestMapping("/cmsservice/admin-banner")
@Slf4j
@CrossOrigin // 跨域。包括ip、端口号、
public class BannerAdminController {
  @Autowired
  CrmBannerService crmBannerService;

  @ApiOperation("所有获取幻灯片")
  @GetMapping("{current}/{size}")
  public Result getBanner(@PathVariable Integer current, @PathVariable Integer size) {
    Page<CrmBanner> page = new Page<>(current, size);
    crmBannerService.page(page, null);
    List<CrmBanner> bannerList = page.getRecords();
    long total = page.getTotal();
    long pages = page.getPages();
    log.info("获取当前幻灯片数据：第" + current + "页，总共" + pages + "页，" + total + "条数据。" + bannerList.toString());
    return Result.success().addData("data", bannerList).addData("total", total).addData("pages", pages);
  }

  @PutMapping("/")
  public Result addBanner(@RequestBody CrmBanner crmBanner) {
    boolean isSuccess = crmBannerService.save(crmBanner);
    if (isSuccess) {
      log.info("添加成功：" + crmBanner.toString());
      return Result.success();
    } else {
      log.error("添加失败：" + crmBanner.toString());
      return Result.failure();
    }
  }

  @PostMapping("/{id}")
  public Result updateBanner(@PathVariable String id, @RequestBody CrmBanner crmBanner) {
    crmBanner.setId(id);
    boolean isSuccess = crmBannerService.updateById(crmBanner);
    if (isSuccess) {
      log.info("修改成功：" + crmBanner.toString());
      return Result.success();
    } else {
      log.error("修改失败：" + crmBanner.toString());
      return Result.failure();
    }
  }

  @DeleteMapping("/{id}")
  public Result removeBanner(@PathVariable String id) {
    boolean isSuccess = crmBannerService.removeById(id);
    if (isSuccess) {
      log.info("删除成功，id：" + id);
      return Result.success();
    } else {
      log.error("删除失败，id：" + id);
      return Result.failure();
    }
  }
}

